package danny.hoi.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import danny.hoi.domain.export.cargo.OutputQueryVo;
import danny.hoi.service.cargo.ContractProductService;
import danny.hoi.web.controller.base.BaseController;
import danny.hoi.web.utils.DownLoadUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class OutProductController extends BaseController {
    @Reference
    private ContractProductService contractProductService;

    /**
     * 1.访问出货表页面
     * 2.请求地址：/cargo/contract/print.do
     * 3.响应地址：WEB-INF/pages/cargo/print/contract-print.jsp
     */
    @RequestMapping("/print")
    public String toPrint() {
        return "cargo/print/contract-print";
    }

    /**
     * 1.下载登陆公司的货物船期某个月份内的所有出货表
     * 2.请求地址：/cargo/contract/printExcel.do
     * 参数：inputDate
     */
    @Autowired
    private DownLoadUtils downLoadUtils;

    @RequestMapping("/printExcel")
    public void printExcel(String inputDate) {
        //查询货物数据
        List<OutputQueryVo> list =
                contractProductService.findByShipTime(getLoginCompanyId(), inputDate);
        //获取输出模板
        InputStream resource =
                session.getServletContext().getResourceAsStream("/make/xlsprint/tOUTPRODUCT.xlsx");
        Workbook workbook = null;
        try {
            //获取模板工作簿
            workbook = new XSSFWorkbook(resource);
            //获取工作表
            Sheet sheet = workbook.getSheetAt(0);
            //设置表头
            Row row = sheet.getRow(0);
            Cell cell = row.createCell(1);
            cell.setCellValue(inputDate.replace("-0", "-")
                    .replace("-", "年") + "月份出货表");
            //封装工作表数据
            int count = 2;
            if (list != null && list.size() > 0) {
                for (OutputQueryVo vo : list) {
                    row = sheet.createRow(count++);
                    //填充数据
                    cell = row.createCell(1);
                    cell.setCellValue(vo.getCustomName());

                    cell = row.createCell(2);
                    cell.setCellValue(vo.getContractNo());

                    cell = row.createCell(3);
                    cell.setCellValue(vo.getProductNo());

                    cell = row.createCell(4);
                    cell.setCellValue(vo.getCnumber());

                    cell = row.createCell(5);
                    cell.setCellValue(vo.getFactoryName());

                    cell = row.createCell(6);
                    cell.setCellValue(vo.getDeliveryPeriod());

                    cell = row.createCell(7);
                    cell.setCellValue(vo.getShipTime());

                    cell = row.createCell(8);
                    cell.setCellValue(vo.getTradeTerms());
                }
            }
            //下载数据
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            workbook.write(bos);
            downLoadUtils.download(bos, response, "出货表.xlsx");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resource != null) {
                    resource.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
